package cn.ffcs.uec.post.component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.Getter;
import lombok.Setter;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.SuspendNotAllowedException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.post.component.bean.UecPostListboxExt2Bean;
import cn.ffcs.uec.post.manager.UecPostManager;
import cn.ffcs.uec.post.model.UecPost;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.NumericUtil;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.NodeVo;
import cn.ffcs.uom.common.vo.PageInfo;

@Controller
@Scope("prototype")
public class UecPostListboxExt2 extends Div implements IdSpace {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8131412059457103717L;

	private UecPostListboxExt2Bean bean = new UecPostListboxExt2Bean();
	/**
	 * zul.
	 */
	private static final String zul = "/pages_nmg/uec_post/comp/uecPost_listbox_ext.zul";

	private UecPostManager uecPostManager = (UecPostManager) ApplicationContextUtil
			.getBean("uecPostManager");

	private UecPost uecPost;

	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;

	public UecPostListboxExt2() {
		// 1.绑定页面
		Executions.createComponents(zul, this, null);
		// 2.页面控件与java属性绑定
		Components.wireVariables(this, bean);
		// 3.绑定监听事件
		Components.addForwards(this, this, '$');

	}

	/**
	 * 初始化
	 */
	public void onCreate() {
		List<NodeVo> nodeVoList = new ArrayList<NodeVo>();
		NodeVo nodeVo = new NodeVo();
		nodeVo.setName("岗位名称");
		nodeVo.setId("1");

		NodeVo nodeVo2 = new NodeVo();
		nodeVo2.setName("岗位编码");
		nodeVo2.setId("2");

		nodeVoList.add(nodeVo);
		nodeVoList.add(nodeVo2);

		ListboxUtils.rendererForEdit(bean.getCondition(), nodeVoList);
		setButtonValid(false, false, false);
	}

	/**
	 * 设置按钮
	 * 
	 * @param canAdd
	 * @param candel
	 */
	private void setButtonValid(boolean canAdd, boolean canEdit, boolean candel) {
		this.bean.getAddButton().setDisabled(!canAdd);
		this.bean.getEditButton().setDisabled(!canEdit);
		this.bean.getDelButton().setDisabled(!candel);
	}

	/**
	 * 查询按钮
	 */
	public void onQueryUecPost() {
		// this.bean.getUecPartyLogListboxPaging().setActivePage(0);
		clearVariable();

		// PubUtil.clearComponent(this.bean.getBasePanel());
		if (!StrUtil.isNullOrEmpty(bean.getCondition().getSelectedItem()
				.getValue())
				&& !StrUtil.isNullOrEmpty(bean.getConditionValue().getValue())) {
			Listbox listbox = bean.getCondition();
			String listboxStr = (String) listbox.getSelectedItem().getValue();
			int listboxId = Integer.parseInt(listboxStr);
			uecPost = UecPost.newInstance();
			switch (listboxId) {
			case 1:
				uecPost.setPostname(bean.getConditionValue().getValue());
				break;
			case 2:
				uecPost.setPostcode(bean.getConditionValue().getValue());
				break;
			default:
				break;
			}
		}

		queryPagingPost();
	}
	
	private void queryPagingPost() {
		int activePage = bean.getUecPostListboxPaging().getActivePage();
		int pageSize = bean.getUecPostListboxPaging().getPageSize();
		PageInfo pageInfo = uecPostManager.queryPageInfoByUecPost(uecPost,
				activePage + 1, pageSize);
		if (null != pageInfo) {
			ListModelList dataList = new BindingListModelList(
					pageInfo.getDataList(), true);
			bean.getUecPostListbox().setModel(dataList);
			bean.getUecPostListboxPaging().setTotalSize(
					NumericUtil.nullToZero(pageInfo.getTotalCount()));
		} else {
			bean.getUecPostListboxPaging().setActivePage(0);
			bean.getUecPostListboxPaging().setTotalSize(1);
			PubUtil.clearListbox(bean.getUecPostListbox());
		}
		
	}

	/**
	 * 重置按钮
	 */
	public void onResetUecPost() {
		clearVariable();
		bean.getUecPostListboxPaging().setActivePage(0);
		bean.getUecPostListboxPaging().setTotalSize(1);
		bean.getConditionValue().setValue(null);
		PubUtil.clearComponent(this);

	}

	/**
	 * 选择一条记录
	 */
	public void onSelectRequest() {
		int selCount = bean.getUecPostListbox().getSelectedCount();
		if (selCount > 0) {
			uecPost = (UecPost) bean.getUecPostListbox().getSelectedItem()
					.getValue();
			
//			setButtonValid(true, true, false);
			openUecEditWin("view");
		}
	}

	/**
	 * 
	 * @param opType
	 */
	private void openUecEditWin(String opType) {

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("opType", opType);
		if ("view".equals(opType) || "edit".equals(opType)) {
			if (null != uecPost) {
				map.put("uecPost", uecPost);
			} else {
				ZkUtil.showError("请选择一条记录！", "提示信息");
				return;
			}
		}
		Window window = (Window) Executions.createComponents(
				"/pages_nmg/uec_post/comp/uec_post_edit.zul", this, map);
		try {
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
//						setButtonValid(true, true, false);
						uecPost = (UecPost) event.getData();
					}
				}
			});
		} catch (SuspendNotAllowedException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 新增按钮
	 * 
	 * @throws Exception
	 * @throws
	 */
	public void onAddUecPost() throws Exception {
		openUecEditWin("add");
	}

	/**
	 * 编辑按钮
	 * 
	 * @throws Exception
	 * @throws InterruptedException
	 */
	public void onEditUecPost() throws Exception, InterruptedException {
		openUecEditWin("edit");
	}

	/**
	 * 删除按钮
	 */
	public void onDelUecPost() {
		if (this.uecPost != null) {
			ZkUtil.showQuestion("确定要删除吗?", "提示信息", new EventListener() {
				public void onEvent(Event event) throws Exception {
					Integer result = (Integer) event.getData();
					if (result == Messagebox.OK) {
						// uecPartyLog.setId(id);
						uecPost.removeOnly();
						uecPost = null;
						onQueryUecPost();
						Messagebox.show("删除成功！");
					}
				}
			});
		} else {
			ZkUtil.showError("请选择你要删除的记录", "提示信息");
			return;
		}
	}
	
	public void onListboxPaging() {
		queryPagingPost();
	}

	/**
	 * 清除变量
	 */
	public void clearVariable() {
		uecPost = null;
	}

}
